package com.bosssoft.trainee.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.trainee.common.annotation.Description;
import com.bosssoft.trainee.mbg.dto.ResourceDTO;
import com.bosssoft.trainee.mbg.po.Resource;
import com.bosssoft.trainee.mbg.vo.ResourceVO;
import com.bosssoft.trainee.mbg.result.CommonResult;
import com.bosssoft.trainee.service.ResourceService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @Author: wuxin
 * @Date: 2022/6/17
 * @Project: rbac
 * @Description:
 */
@RestController
@RequestMapping(value = "/reso")
@Validated
public class ResourceController {
    @Autowired
    ResourceService resourceService;

    @Description("获取资源列表")
    @GetMapping(value = "/list")
    public CommonResult<Page<Resource>> getList(@RequestParam(value = "example", required = false) ResourceVO resourceVO,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum
    ) {
        ResourceDTO example = new ResourceDTO();
        BeanUtil.copyProperties(resourceVO, example);
        return CommonResult.success(resourceService.getList(pageSize, pageNum, example));
    }

    @Description("根据Id删除资源")
    @GetMapping(value = "/deleteById")
    public CommonResult<String> deleteResourceById(@NotNull(message = "参数id不能为空") @RequestParam("id") Integer id) {
        return resourceService.deleteById(id) > 0 ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }

    @Description("添加资源")
    @PostMapping(value = "/addReso")
    public CommonResult<String> addResource(@Validated @RequestBody ResourceVO resourceVO) {
        ResourceDTO resourceDTO = new ResourceDTO();
        BeanUtil.copyProperties(resourceVO, resourceDTO);
        return resourceService.addResource(resourceDTO) > 0 ? CommonResult.success("添加成功") : CommonResult.failed("添加失败");
    }

    @Description("修改资源")
    @PostMapping(value = "/updateReso")
    public CommonResult<String> updateResource(@Validated @RequestBody ResourceVO resourceVO) {
        if (resourceVO.getId() == null) {
            return CommonResult.failed("修改失败,id不能为空");
        }
        ResourceDTO resourceDTO = new ResourceDTO();
        BeanUtil.copyProperties(resourceVO, resourceDTO);
        return resourceService.updateResource(resourceDTO) > 0 ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    @Description("检查资源代码")
    @GetMapping(value = "/checkCode")
    public CommonResult<Boolean> checkCode(@NotBlank(message = "不能为空")
                                           @Pattern(regexp = "^\\d{4}$", message = "格式不正确")
                                           @RequestParam("code") String code) {
        return CommonResult.success(resourceService.checkCode(code));
    }

    @Description("检查资源Api是否被占用")
    @GetMapping(value = "/checkApi")
    public CommonResult<Boolean> checkApi(@NotBlank(message = "不能为空")
                                          @Length(max = 10, message = "资源url长度不得多于10个字符")
                                          @Pattern(regexp = "^((?=[a-zA-Z/]).){1,10}$", message = "格式不正确")
                                          @RequestParam("api") String api) {

        return CommonResult.success(resourceService.checkCode(api));
    }

    @Description("检查资源Url是否被占用")
    @GetMapping(value = "/checkUrl")
    public CommonResult<Boolean> checkUrl(@NotBlank(message = "不能为空")
                                          @Length(max = 10, message = "资源url长度不得多于10个字符")
                                          @Pattern(regexp = "^((?=[a-zA-Z/]).){1,10}$", message = "格式不正确")
                                          @RequestParam("url") String url) {
        return CommonResult.success(resourceService.checkCode(url));
    }
}
